<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree
          default-expand-all
          :data="list"
        >
          <!-- 使用作用域插槽获取组件内部的数据,可以使用提供的默认的作用域插槽==>{node,data} 接收内部数据 -->
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <svg-icon icon-class="tree" /><span> &nbsp; {{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length===0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <!-- 新增子部门或者编辑部门的弹层 -->
      <el-dialog
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :title="isEdit?'编辑部门':'新增子部门'"
        @close="hClose"
      >
        <!-- v-if="showDialog" 解决数据不更新方式 1111 -->
        <addOrEdit
          v-if="showDialog"
          :id="curId"
          ref="myAddRef"
          :is-edit="isEdit"
          :origin-list="originList"
          @success="hSuccess"
          @close="showDialog=false"
        />
      </el-dialog>
    </div>
  </div>

</template>
<script>
import { getDepartments, delDepartments } from '@/api/departments'
import { tranDataToTree } from '@/utils'
import addOrEdit from '@/views/departments/deptDialog.vue'
export default {
  components: {
    addOrEdit
  },
  data() {
    return {
      showDialog: false, // 控制弹层的显示和隐藏
      list: [], // 保存后台返回的数据部门列表
      curId: '', // 当前点击的部门的id
      isEdit: false, // false 新增 || true 编辑
      originList: [] // 用于表单校验需要的数据
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    async loadDepartments() {
      try {
        const res = await getDepartments()
        // console.log('loadDepartments', res)
        this.originList = res.data.depts.map(item => {
          return { id: item.id, code: item.code, name: item.name, pid: item.pid }
        })
        // console.log(this.originList)
        // 删除数组中第一条数据 公司的名称
        res.data.depts.shift()
        this.list = tranDataToTree(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    },
    // 点击确定
    hAdd(id) {
      this.curId = id
      // 打开弹层
      this.showDialog = true
      this.isEdit = false
    },
    // 子传父
    hSuccess() {
      // 重新渲染页面
      this.loadDepartments()
      // 关闭弹层
      this.showDialog = false
    },
    hEdit(id) {
      this.curId = id
      this.isEdit = true // 编辑状态
      // 解决共用组件数据不跟新的方式 22222
      // 可以在父组件中调用子组件中的获取部门详情的数据填充到表单
      // this.$nextTick(() => {
      //   this.$refs.myAddRef.loadDepartments()
      // })
      this.showDialog = true
    },
    // 根据id删除部门
    hDel(id) {
      this.$confirm('您确定删除此部门吗???').then(async() => {
        const res = await delDepartments(id)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.loadDepartments()
      }).catch(err => err)
    },
    hClose() {
      // 重置表单,清空表单
      this.$refs.myAddRef.resetForm()
    }
  }
}

</script>
